﻿/// <reference path="../GeneratedArtifacts/viewModel.js" />
myapp.AddEditOrder.AvailableItems_postRender = function (element, contentItem) {
    // Get the Order on the screen
    var order = contentItem.screen.Order;
    // Show the current count
    element.innerText = order.getAvailable();

    // Method that will be called on update of count
    function updateValue() {
        element.innerText = order.getAvailable();
    }

    // Add event listener
    order.OrderDetails.addEventListener(
        "collectionchange", updateValue);

    // Trigger load of OrderDetails
    order.getOrderDetails();

    // Clean up event listener
    contentItem.handleViewDispose(function () {
        parent.Children.removeEventListener(
            "collectionchange", updateValue);
    });
};

myapp.AddEditOrder.UnAvailableItems_postRender = function (element, contentItem) {
    // Get the Order on the screen
    var order = contentItem.screen.Order;
    // Show the current count
    element.innerText = order.getUnAvailable();

    // Method that will be called on update of count
    function updateValue() {
        element.innerText = order.getUnAvailable();
    }

    // Add event listener
    order.OrderDetails.addEventListener(
        "collectionchange", updateValue);

    // Trigger load of OrderDetails
    order.getOrderDetails();

    // Clean up event listener
    contentItem.handleViewDispose(function () {
        parent.Children.removeEventListener(
            "collectionchange", updateValue);
    });
};

myapp.AddEditOrder.GetChanges_execute = function (screen) {
    // Clear screen.strChanges
    screen.strChanges = "";

    // Get the Changes
    var ChangeSet = myapp.activeDataWorkspace.ApplicationData.details.getChanges();

    // Loop through each change
    $(ChangeSet).each(function (i, OrderDetail) {
        // Display the change
        screen.strChanges = screen.strChanges + "<h2>" + OrderDetail.ProductName + "</h2>";
    });
};

myapp.AddEditOrder.strChanges_render = function (element, contentItem) {
    // We create a binding to "value"
    // When the data is loaded the binding will be raised
    contentItem.dataBind("value", function (newValue) {
        // Set the newValue as the innerHTML of the element
        if (newValue != undefined) {
            element.innerHTML = newValue;
        }
    });
};
